﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using Windows.ApplicationModel.Store;

namespace Undercover.Model
{
    /// <summary>
    /// Logical Word Package, which is stored on local disk.
    /// It is associated with some in-app product by ProductID.
    /// 
    /// There are three kinds of Package:
    ///     Internal and Free,
    ///     Internal and Purchase-In-Need
    ///     External and always Purchase-In-Need
    ///     
    /// Internal means this package is distributed with the app (may store as static resources or hardcode).
    /// External means this package should be downloaded from app store.
    /// 
    /// Internal package will be loaded from local disk or memory (if hardcoded), so initial storepolicy should be Local.
    /// External package will be loaded from internet, the initial store-policy should be Internet.
    /// Internal or External package will be stored in LocalEncryption finally.
    /// </summary>
    public class WordPackage
    {
        #region fields for serialization

        /// <summary>
        /// The display name of the package
        /// </summary>
        public string DisplayName { get; set; }

        /// <summary>
        /// Indicates if the package is selected by user as the treasure to play.
        /// </summary>
        public bool Selected { get; set; }

        /// <summary>
        /// This field is used to associate with in-app product.
        /// For user defined package or internal free package, this field should be less than 12 letters.
        /// </summary>
        public string ProductID { get; set; }

        /// <summary>
        /// Indicates if this package is purchased.
        /// </summary>
        public bool Purchased { get; set; }

        /// <summary>
        /// The cache for wordpair list so that the app doesn't need to download it from internet everytime.
        /// </summary>
        public WordPairList _memoryCache;  // TO-DO: should use data contract so this field can be private.

        #endregion

        /// <summary>
        /// Indicates if this package is required to purchase.
        /// </summary>
        public bool RequiredToPurchase { get { return !Purchased; } }

        /// <summary>
        /// Indicates if this package is free.
        /// </summary>
        public bool IsFree { get { return String.IsNullOrEmpty(ProductID) || ProductID.Length < 12; } }
        
        /// <summary>
        /// Returns the count of words that have been used.
        /// </summary>
        public int UseWordCount { get { return (_memoryCache == null) ? 0 : _memoryCache.Count(it => it.UseCount > 0); } }

        /// <summary>
        /// Returns the count of word in the package.
        /// </summary>
        public int WordCount { get { return (_memoryCache == null) ? 0 : _memoryCache.Count; } }

        public WordPackage()
        {
        }

        /// <summary>
        /// Gets the list of the word pairs in this package.
        /// </summary>
        /// <returns></returns>
        public WordPairList GetWordPairList()
        {
            if (!Purchased)
            {
                // should ask user to buy it.
                throw new NotImplementedException();
            }

            if (_memoryCache == null)
            {
                // try to load from local disk
            }

            return _memoryCache;
        }

        public bool Purchase(ProductLicense license)
        {
            if (license != null && license.IsActive && license.ProductId == this.ProductID)
            {
                if (_memoryCache == null)
                {
                    /// TO-DO:
                    /// for external package, we should get the actual word pair from my service.      
                }

                Purchased = true;
                return true;
            }

            return false;
        }

        internal static WordPackage EmptyUserDefinedPackage = new WordPackage()
        { Purchased = false, ProductID = null, DisplayName = "User defined package", Selected = true };

        internal static List<WordPackage> GetInternalPackages()
        {
            List<WordPackage> ret = new List<WordPackage>();
            
            WordPackage package = new WordPackage() { DisplayName = "初级", Selected = true, Purchased = true, ProductID = "basic-1" };
            package._memoryCache = new WordPairList();
            package._memoryCache.Add(new WordPair() { MajorityWord = "苹果", MinorityWord = "香蕉", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "手机", MinorityWord = "钱包", CanSwap = false });
            package._memoryCache.Add(new WordPair() { MajorityWord = "香烟", MinorityWord = "啤酒", CanSwap = true });
            ret.Add(package);

            package = new WordPackage() { DisplayName = "中级", Selected = false, Purchased = true, ProductID = "basic-2" };
            package._memoryCache = new WordPairList();
            package._memoryCache.Add(new WordPair() { MajorityWord = "党员", MinorityWord = "同志", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "耳机", MinorityWord = "眼镜", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "牛仔", MinorityWord = "部落", CanSwap = false });
            ret.Add(package);

            package = new WordPackage() { DisplayName = "高级", Selected = false, Purchased = true, ProductID = "basic-3" };
            package._memoryCache = new WordPairList();
            package._memoryCache.Add(new WordPair() { MajorityWord = "蜡烛", MinorityWord = "老师", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "闺蜜", MinorityWord = "票友", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "中南海", MinorityWord = "北载河", CanSwap = true });
            ret.Add(package);

            package = new WordPackage() { DisplayName = "小清新", Selected = false, Purchased = false, ProductID = "Undercover-w2-wordpackage-light" };
            package._memoryCache = new WordPairList();
            package._memoryCache.Add(new WordPair() { MajorityWord = "小红帽", MinorityWord = "喜洋洋", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "口袋", MinorityWord = "抽屉", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "天津", MinorityWord = "上海", CanSwap = true });
            ret.Add(package);

            package = new WordPackage() { DisplayName = "重口味", Selected = false, Purchased = false, ProductID = "Undercover-w2-wordpackage-hardcore" };
            package._memoryCache = new WordPairList();
            package._memoryCache.Add(new WordPair() { MajorityWord = "墙角", MinorityWord = "小三", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "美女", MinorityWord = "毒蛇", CanSwap = true });
            package._memoryCache.Add(new WordPair() { MajorityWord = "八戒", MinorityWord = "吴刚", CanSwap = false });
            ret.Add(package);
            
            return ret;
        }
    }
}
